package com.zhifei.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhifei.system.domain.OnesProjectEntity;
import com.zhifei.system.domain.OnesProjectManhoursEntity;
import com.zhifei.system.mapper.OnesProjectManhoursMapper;
import com.zhifei.system.mapper.OnesProjectMapper;
import com.zhifei.system.service.OnesProjectManhoursService;
import com.zhifei.system.service.OnesProjectService;
import com.zhifei.system.util.ones.OnesProjectManhoursInterfaceVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OnesProjectManhoursServiceImpl extends ServiceImpl<OnesProjectManhoursMapper, OnesProjectManhoursEntity> implements OnesProjectManhoursService {

    @Autowired
    private OnesProjectManhoursMapper onesProjectManhoursMapper;

    @Override
    public void saveProjectManhours(List<OnesProjectManhoursInterfaceVo> onesProjectManhoursInterfaceVoList) {
        List<OnesProjectManhoursEntity> onesProjectManhoursEntityList = new ArrayList<>();
        List<String> ManhoursInterfaceIdList = onesProjectManhoursInterfaceVoList.stream().map(OnesProjectManhoursInterfaceVo::getManhoursId).collect(Collectors.toList());
        List<String> inManhoursIdList = this.lambdaQuery().in(OnesProjectManhoursEntity::getManhoursId,ManhoursInterfaceIdList).list().stream().map(OnesProjectManhoursEntity::getManhoursId).collect(Collectors.toList());
        onesProjectManhoursInterfaceVoList = onesProjectManhoursInterfaceVoList.stream().filter(manhours -> !inManhoursIdList.contains(manhours.getManhoursId())).collect(Collectors.toList());
        for(OnesProjectManhoursInterfaceVo onesProjectManhoursInterfaceVo : onesProjectManhoursInterfaceVoList){
            OnesProjectManhoursEntity onesProjectManhoursEntity = new OnesProjectManhoursEntity();
            BeanUtils.copyProperties(onesProjectManhoursInterfaceVo,onesProjectManhoursEntity);
            onesProjectManhoursEntityList.add(onesProjectManhoursEntity);
        }
        if(onesProjectManhoursEntityList!=null && onesProjectManhoursEntityList.size()>0){
            this.saveBatch(onesProjectManhoursEntityList);
        }
    }
}
